package com.weimob.mcloud.wechatwork.customer.mq.handler.losemsg;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.bos.response.ProductInstanceDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.request.QueryGoodsListAdapterRequest;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.mall.response.QueryGoodsListAdapterResponse;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.page.PageInfoResponse;
import com.weimob.mcloud.wechatwork.core.adapter.service.MallAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.customer.domain.CustomerTouchInfo;
import com.weimob.mcloud.wechatwork.customer.facade.impl.sidebar.SidebarFacade;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 商品侧边栏
 * Create Time: 2022年05月23日 10:14
 *
 * @author maosheng.li
 **/
@Service
@Slf4j
public class GoodsTouchImpl implements CustomerTouchService {

    @Value("${mall.productid}")
    private Long mallProductId;

    @Autowired
    private SidebarFacade sidebarFacade;

    @Resource
    private MallAdapterService mallAdapterService;
    @Override
    public CustomerTouchInfo getCustomerTouchInfo(PrivateSphere privateSphere,CustomerTouchInfo touchInfoRequest) {
        CustomerTouchInfo result = new CustomerTouchInfo();

        ProductInstanceDTO productByVid = sidebarFacade.getProductByVid(touchInfoRequest.getEcVid(), mallProductId);
        if (Objects.isNull(productByVid)) {
            return null;
        }
        QueryGoodsListAdapterRequest queryGoodsListAdapterRequest = new QueryGoodsListAdapterRequest();
        queryGoodsListAdapterRequest.setBosId(privateSphere.getBosId());
        queryGoodsListAdapterRequest.setMerchantId(productByVid.getMerchantId());
        queryGoodsListAdapterRequest.setProductId(mallProductId);
        queryGoodsListAdapterRequest.setProductInstanceId(productByVid.getProductInstanceId());
        queryGoodsListAdapterRequest.setVid(touchInfoRequest.getEcVid());
        queryGoodsListAdapterRequest.setGoodsIdList(Lists.newArrayList(Long.valueOf(touchInfoRequest.getTaskId())));
        queryGoodsListAdapterRequest.setPageNum(1);
        queryGoodsListAdapterRequest.setPageSize(1);
        PageInfoResponse<QueryGoodsListAdapterResponse> unpackResponse;
        try {
            log.info("查询商品列表接口: request:{}", JSON.toJSONString(queryGoodsListAdapterRequest));
            unpackResponse = SoaUtil.unpack(mallAdapterService.queryGoodsListWithPageForManagement(queryGoodsListAdapterRequest));
            log.info("查询商品列表接口: response:{}", JSON.toJSONString(unpackResponse));
            if(CollectionUtils.isNotEmpty(unpackResponse.getDataList())){
                QueryGoodsListAdapterResponse queryGoodsListAdapterResponse = unpackResponse.getDataList().get(0);
                result.setTaskName(queryGoodsListAdapterResponse.getTitle());
            }

        } catch (Exception e){
            log.error("获取商品列表的接口发生异常，错误内容：", e);
        }
        return result;
    }
}
